package com.lwbldy.modules.student.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lwbldy.common.tools.R;
import com.lwbldy.modules.student.domain.ChargesItems;
import com.lwbldy.modules.student.domain.ChargesType;
import com.lwbldy.modules.student.domain.Major;
import com.lwbldy.modules.student.mapper.ChargesItemsDao;
import com.lwbldy.modules.student.mapper.ChargesTypeDao;
import com.lwbldy.modules.student.mapper.MajorDao;
import com.lwbldy.modules.student.service.MajorService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 专业 service 实现
 * @author lwb
 * @email lwbldy@163.com
 * @date 2022-06-13 10:33:21
 */
@Service("majorService")
public class MajorServiceImpl implements MajorService {

    @Autowired
    MajorDao majorDao;
    @Autowired
    ChargesItemsDao chargesItemsDao;
    @Autowired
    ChargesTypeDao chargesTypeDao;

    @Override
    public int save(Major t) {
        return majorDao.save(t);
    }

    @Override
    public int saveBatch(List<Major> list) {
        return majorDao.saveBatch(list);
    }


    @Override
    @Transactional
    public int delete(Object id) {
        chargesItemsDao.deleteByMajorId(id);
        return majorDao.delete(id);
    }

    @Override
    public int deleteBatch(Object[] ids) {
        return majorDao.deleteBatch(ids);
    }

    @Override
    public Major queryObject(Object id) {
        return majorDao.queryObject(id);
    }

    @Override
    public List<Major> queryList(Map<String, Object> map) {
        return majorDao.queryList(map);
    }

    @Override
    public int queryTotalByMap(Map<String, Object> map) {
        return majorDao.queryTotalByMap(map);
    }

    @Override
    public int queryTotal() {
        return majorDao.queryTotal();
    }

    @Override
    @Transactional
    public R saveMajor(Major major, String[] charges_name, String[] money) {
        int r = majorDao.save(major);
        if(r != 1){
            return R.error("保存专业失败");
        }

        if(money == null || money.length == 0){
            return R.error("请至少填写一个金额");
        }

        for(int i = 0;i < money.length;i++){
            try{
                double mon = Double.parseDouble(money[i]);
                if(mon <= 0){
                    return R.error("收费金额请填写正确的数值");
                }
            }catch (Exception e){
                return R.error("收费金额请填写正确的数值");
            }
        }

        saveTypes(charges_name,money,major);

        return R.ok();
    }

    @Override
    @Transactional
    public R update(Major t,String[] charges_name,String[] money) {

        if(money == null || money.length == 0){
            return R.error("请至少填写一个金额");
        }

        int r = majorDao.update(t);

        // 删除收费项
        chargesItemsDao.deleteByMajorId(t.getId());

        if(r != 1){
            return R.error("保存专业失败");
        }

        for(int i = 0;i < money.length;i++){
            try{
                double mon = Double.parseDouble(money[i]);
                if(mon <= 0){
                    return R.error("收费金额请填写正确的数值");
                }
            }catch (Exception e){
                return R.error("收费金额请填写正确的数值");
            }
        }
        saveTypes(charges_name,money,t);
        return R.ok();
    }

    private void saveTypes(String[] charges_name,String[] money,Major major){
        for(int i = 0;i < money.length;i++){
            ChargesItems c = new ChargesItems();
            String code = charges_name[i];
            c.setChargesCode(code);
            c.setMajorId(major.getId());
            double mon = Double.parseDouble(money[i]);

            ChargesType chargesType = chargesTypeDao.queryByCode(code,major.getSchoolId());
            if(chargesType == null){
                continue;
            }
            c.setChargesName(chargesType.getName());
            c.setMoney(mon);
            chargesItemsDao.save(c);
        }
    }


    @Override
    public List<Major> queryByLevelId(int levelId) {
        return majorDao.queryByLevelId(levelId);
    }

    @Override
    public List<Major> queryBySchoolId(int schoolId) {
        return majorDao.queryBySchoolId(schoolId);
    }

    @Override
    public int changeShow(int id, int isShow) {
        return majorDao.changeShow(id,isShow);
    }

}
